﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：TableFieldType.cs
           // 文件功能描述：表字段的字段类型。
           //
           // 
           // 创建标识：丁余良 20080927
           //
           // 修改标识
           // 修改描述：
           //
           // 修改标识：
           // 修改描述：
----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using RuleCode.ObjectTree;

namespace RuleCode.Common
{
    #region 基类
    /// <summary>
    /// 表字段类型的基类
    /// </summary>
    public abstract class TableFieldType : Object
    {
        #region 当前字段所在的表
        private Table table;
        /// <summary>
        /// 获取或设置当前字段所在的表 
        /// </summary>
        public Table Table
        {
            get
            {
                return this.table;
            }
            set { this.table = value; }
        }
        #endregion

        #region 虚方法和属性
        /// <summary>
        /// 获取是否是默认值
        /// </summary>
        public virtual bool IsDefaultValue { get; set; }

        /// <summary>
        /// 设置值得方法调用
        /// </summary>
        /// <param name="value"></param>
        public virtual void SetValue(object value)
        {
            //if (this.BaseProperty != null && this.BaseProperty.Mandatory == true)//在valid事件里面去检查，yujie.zhou
            //{
            //    if (this.StringValue == null || this.StringValue.Trim() == "")
            //        throw new InvalidOperationException("字段:\"" + this.BaseProperty.Name + "\" 为必填，不能为空！");
            //}
            //if (this.BaseProperty != null&&this.BaseProperty.AllowEdit == false )
            //{
            //    throw new InvalidOperationException("字段不能修改！");
            //}
        }
        /// <summary>
        /// 用于设置值
        /// </summary>
        public virtual object ObjectValue { get; set; }
        /// <summary>
        /// 由于界面显示，尤其是GridView 和 TextBox等，子类重载了 object的ToString()方法，便于界面显示
        /// </summary>
        public virtual string StringValue { get; set; }
        /// <summary>
        /// 用于数据库操作时的数据类型转换
        /// </summary>
        public virtual System.Type ValueType { get; set; }
        /// <summary>
        /// 表字段类型 的 扩展类型属性
        /// </summary>
        public virtual System.Type ExtendDataType { get; set; }

        private string label;
        /// <summary>
        /// 多语言显示 
        /// </summary>
        public string ZLabel
        {
            get
            {
                return this.label;
            }
            set { this.label = value; }
        }

        /// <summary>
        /// 获取字段的显示的文本
        /// </summary>
        public string Text
        {
            get
            {
                if (this.BaseProperty == null)
                    return "";
                else
                    return SystemLanguage.Instance.GetLabel(this.BaseProperty.Label);
            }
        }

        /// <summary>
        /// 表字段在对象树中的 属性列表
        /// </summary>
        public virtual TableFieldProperty BaseProperty { get; set; }
        public virtual TableFieldType GetDefaultValue() { return null; }
        #endregion

        #region LookUp 事件
        public event LookUpHandler LookUp;
        protected virtual void OnLookUp(LookUpEventArg e)
        {
            if (this.LookUp != null)
                LookUp(this, e);
        }
        public void RaiseLookUp(LookUpEventArg e)
        {
            this.OnLookUp(e);
        }
        #endregion

        #region 静态属性
        private static Dictionary<System.Type, TableFieldType> defaultValueHashTable;
        /// <summary>
        /// 获取 子类默认值的集合
        /// </summary>
        public static Dictionary<System.Type, TableFieldType> DefaultValueHashTable
        {
            get
            {
                if (defaultValueHashTable == null)
                {
                    defaultValueHashTable = new Dictionary<Type, TableFieldType>();
                    defaultValueHashTable.Add(typeof(TInt), TInt.DefaultValue);
                    defaultValueHashTable.Add(typeof(TInt64), TInt64.DefaultValue);
                    defaultValueHashTable.Add(typeof(TString), TString.DefaultValue);
                    defaultValueHashTable.Add(typeof(TDouble), TDouble.DefaultValue);
                    defaultValueHashTable.Add(typeof(TFloat), TFloat.DefaultValue);
                    defaultValueHashTable.Add(typeof(TEnum), TEnum.DefaultValue);
                    defaultValueHashTable.Add(typeof(TTime), TTime.DefaultValue);
                    defaultValueHashTable.Add(typeof(TDate), TDate.DefaultValue);
                    defaultValueHashTable.Add(typeof(TContainer), TContainer.DefaultValue);
                }
                return defaultValueHashTable;
            }
        }
        #endregion

        #region 辅助方法
        /// <summary>
        /// 根据字段类型把value值转换为字符串类型
        /// </summary>
        /// <param name="fieldType">字段类型</param>
        /// <param name="value">要转化的值</param>
        /// <returns>转化后的字符串的值</returns>
        public string ObjectToString(System.Type fieldType, object value)
        {
            if (fieldType.BaseType == typeof(TInt) || fieldType == typeof(TInt))
                return TInt.ObjectToString(value);
            else if (fieldType.BaseType == typeof(TInt64) || fieldType == typeof(TInt64))
                return TInt64.ObjectToString(value);
            else if (fieldType.BaseType == typeof(TString) || fieldType == typeof(TString))
                return TString.ObjectToString(value);
            else if (fieldType.BaseType == typeof(TFloat) || fieldType == typeof(TFloat))
                return TFloat.ObjectToString(value);
            else if (fieldType.BaseType == typeof(TDouble) || fieldType == typeof(TDouble))
                return TDouble.ObjectToString(value);
            else if (fieldType.BaseType == typeof(TDate) || fieldType == typeof(TDate))
                return TDate.ObjectToString(value);
            else if (fieldType.BaseType == typeof(TTime) || fieldType == typeof(TTime))
                return TTime.ObjectToString(value);
            else if (fieldType.BaseType == typeof(TContainer) || fieldType == typeof(TContainer))
                return TContainer.ObjectToString(value);
            else if (fieldType.BaseType == typeof(TEnum) || fieldType == typeof(TEnum))
                return TEnum.ObjectToString(value);
            else
                throw new InvalidCastException("没有与[" + fieldType.Name + "]相对应的类型可以进行转化");
        }
        /// <summary>
        /// 根据字段类型把字符串格式的值转化为值类型的值
        /// </summary>
        /// <param name="fieldType">将要转换的字段的类型</param>
        /// <param name="value">将要转化的字符串格式的值</param>
        /// <returns>转化后的值类型的值</returns>
        public object StringToObject(System.Type fieldType, string value)
        {
            if (fieldType.BaseType == typeof(TInt) || fieldType == typeof(TInt))
                return TInt.StringToObject(value);
            else if (fieldType.BaseType == typeof(TInt64) || fieldType == typeof(TInt64))
                return TInt64.StringToObject(value);
            else if (fieldType.BaseType == typeof(TString) || fieldType == typeof(TString))
                return TString.StringToObject(value);
            else if (fieldType.BaseType == typeof(TFloat) || fieldType == typeof(TFloat))
                return TFloat.StringToObject(value);
            else if (fieldType.BaseType == typeof(TDouble) || fieldType == typeof(TDouble))
                return TDouble.StringToObject(value);
            else if (fieldType.BaseType == typeof(TDate) || fieldType == typeof(TDate))
                return TDate.StringToObject(value);
            else if (fieldType.BaseType == typeof(TTime) || fieldType == typeof(TTime))
                return TTime.StringToObject(value);
            else if (fieldType.BaseType == typeof(TContainer) || fieldType == typeof(TContainer))
                return TContainer.StringToObject(value);
            else if (fieldType.BaseType == typeof(TEnum) || fieldType == typeof(TEnum))
                return TEnum.StringToObject(value);
            else
                throw new InvalidCastException("没有与[" + fieldType.Name + "]相对应的类型可以进行转化");
        }
        #endregion
    }
    #endregion

    #region TInt
    /// <summary>
    /// 数据类型为int
    /// </summary>
    public class TInt : TableFieldType
    {
        #region 辅助方法
        public static string ObjectToString(object value)
        {
            TInt tint = new TInt();
            tint.ObjectValue = value;
            return tint.StringValue;
        }
        public static object StringToObject(string value)
        {
            TInt tint = new TInt();
            tint.StringValue = value;
            return tint.ObjectValue;
        }
        #endregion

        #region 默认值
        public static readonly TInt DefaultValue = new TInt(0);
        /// <summary>
        /// 获取默认值
        /// </summary>
        /// <returns></returns>
        public override TableFieldType GetDefaultValue()
        {
            return DefaultValue;
        }
        /// <summary>
        /// 获取 是否为默认值
        /// </summary>
        public override bool IsDefaultValue
        {
            get
            {
                return (bool)this;
            }
        }
        #endregion

        #region 构造函数
        private int value;
        public TInt() { this.value = DefaultValue.Value; }
        public TInt(int val) { this.value = val; }
        #endregion

        #region 重载方法
        public override bool Equals(object obj) { return base.Equals(obj); }
        public override int GetHashCode() { return base.GetHashCode(); }
        public override string ToString() { return this.StringValue; }
        /// <summary>
        /// 获取值类型
        /// </summary>
        public override System.Type ValueType { get { return typeof(int); } }
        /// <summary>
        /// 获取或设置 值
        /// </summary>
        public override object ObjectValue { get { return this.value; } set { SetValue(value); } }
        /// <summary>
        /// 获取或设置 字符串格式的值
        /// </summary>
        public override string StringValue
        {
            get { return this.value.ToString(); }
            set { SetValue(value); }
        }
        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value">要设置的值,可以是自身类型，或是自身的值的类型，或是字符串类型</param>
        public override void SetValue(object value)
        {
            base.SetValue(value);

            if (value.GetType().IsSubclassOf(typeof(TableFieldType)))
            {
                this.value = Convert.ToInt32(((TableFieldType)value).ObjectValue);
                return;
            }

            //if (value == null || value.ToString() == "")
            //    value  = DefaultValue.Value;

            try
            {
                this.value = Convert.ToInt32(value);
            }
            catch
            {
                throw new InvalidCastException("类型转换不成功:" + value.ToString() + " TO:" + this.GetType().Name);
            }

        }
        #endregion

        #region 属性
        public int Value { get { return this.value; } set { SetValue ( value); } }
        public string ValueString { get { return this.value.ToString(); } }
        public System.Type ControlType { get { return ((new ComboBox()).GetType()); } }
        #endregion

        #region TInt和int之间的显式转换
        public static implicit operator TInt(int x) { return new TInt(x); }
        public static explicit operator int(TInt x) { return x.Value; }
        public static explicit operator bool(TInt x) { return x.Value == DefaultValue.Value; }
        #endregion

        #region 运算符
        public static TInt operator +(TInt x) { return x; }
        public static TInt operator -(TInt x) { return new TInt(-x.value); }

        public static TInt operator +(TInt x, TInt y) { return new TInt(x.value + y.value); }
        public static TInt operator -(TInt x, TInt y) { return new TInt(x.value - y.value); }
        public static TInt operator *(TInt x, TInt y) { return new TInt(x.value * y.value); }
        public static TInt operator /(TInt x, TInt y) { return new TInt(x.value / y.value); }
        public static TInt operator %(TInt x, TInt y) { return new TInt(x.value % y.value); }


        //TInt.Null可以和其他TInt 对象比较操作==,!=；
        public static bool operator ==(TInt x, TInt y) { return x.value == y.value; }
        public static bool operator !=(TInt x, TInt y) { return x.value != y.value; }
        public static bool operator >(TInt x, TInt y) { return x.value > y.value; }
        public static bool operator <(TInt x, TInt y) { return x.value < y.value; }
        public static bool operator >=(TInt x, TInt y) { return x.value >= y.value; }
        public static bool operator <=(TInt x, TInt y) { return x.value <= y.value; }
        #endregion

        #region 基类属性重载
        protected TableFieldIntProperty property;
        /// <summary>
        /// 获取或设置字段在对象树中的 属性列表
        /// </summary>
        public virtual TableFieldIntProperty Property { get; set; }
        /// <summary>
        /// 获取字段在对象数中的 属性列表
        /// </summary>
        public override TableFieldProperty BaseProperty
        { get { return this.Property; } }
        #endregion
    }
    #endregion

    #region TInt64
    /// <summary>
    /// Int64 数据类型
    /// </summary>
    public class TInt64 : TableFieldType
    {
        #region 辅助方法
        public static string ObjectToString(object value)
        {
            TInt64 tInt64 = new TInt64();
            tInt64.ObjectValue = value;
            return tInt64.StringValue;
        }
        public static object StringToObject(string value)
        {
            TInt64 tInt64 = new TInt64();
            tInt64.StringValue = value;
            return tInt64.ObjectValue;
        }
        #endregion

        #region 默认值
        public static readonly TInt64 DefaultValue = new TInt64(0);
        /// <summary>
        /// 获取默认值
        /// </summary>
        /// <returns>TableFieldType 类型的值</returns>
        public override TableFieldType GetDefaultValue()
        {
            return DefaultValue;
        }
        /// <summary>
        /// 获取 是否为默认值
        /// </summary>
        public override bool IsDefaultValue
        {
            get
            {
                return (bool)this;
            }
        }
        #endregion

        #region 构造函数
        private Int64 value;
        public TInt64() { this.value = DefaultValue.Value; }
        public TInt64(Int64 value) { this.value = value; }
        #endregion

        #region 重载
        public override bool Equals(object obj) { return base.Equals(obj); }
        public override int GetHashCode() { return base.GetHashCode(); }
        public override string ToString() { return this.StringValue; }
        /// <summary>
        /// 获取当前值的类型
        /// </summary>
        public override System.Type ValueType { get { return typeof(Int64); } }
        /// <summary>
        /// 获取或设置当前非字符串格式的值
        /// </summary>
        public override object ObjectValue { get { return this.value; } set { SetValue(value); } }
        /// <summary>
        /// 获取或设置当前字符串格式的值
        /// </summary>
        public override string StringValue
        {
            get { return this.value.ToString(); }
            set { SetValue(value); }
        }
        /// <summary>
        ///设置值  
        /// </summary>
        /// <param name="value">可以为TInt64类型 Int64类型以及其他可以转化为Int64类型的值</param>
        public override void SetValue(object value)
        {
            base.SetValue(value);

            if (value.GetType().IsSubclassOf(typeof(TableFieldType)))
            {
                this.value = Convert.ToInt64(((TableFieldType)value).ObjectValue);
                return;
            }

            //if (value == null || value.ToString() == "")
            //    value = DefaultValue.Value;
            try
            {

                this.value = Convert.ToInt64(value);
            }
            catch
            {
                throw new InvalidCastException("类型转换不成功:" + value.ToString() + " TO:" + this.GetType().Name);
            }
        }
        #endregion

        #region 属性
        public Int64 Value { get { return this.value; } set { SetValue(value); } }
        public string ValueString { get { return this.value.ToString(); } }
        public System.Type ControlType { get { return ((new ComboBox()).GetType()); } }
        #endregion

        #region TInt64和Int64之间的显式转换
        public static implicit operator TInt64(Int64 x) { return new TInt64(x); }
        public static explicit operator Int64(TInt64 x) { return x.Value; }
        public static explicit operator bool(TInt64 x) { return x.Value == DefaultValue.Value; }
        #endregion

        #region 运算符
        public static TInt64 operator +(TInt64 x) { return x; }
        public static TInt64 operator -(TInt64 x) { return new TInt64(-x.value); }

        public static TInt64 operator +(TInt64 x, TInt64 y) { return new TInt64(x.value + y.value); }
        public static TInt64 operator -(TInt64 x, TInt64 y) { return new TInt64(x.value - y.value); }
        public static TInt64 operator *(TInt64 x, TInt64 y) { return new TInt64(x.value * y.value); }
        public static TInt64 operator /(TInt64 x, TInt64 y) { return new TInt64(x.value / y.value); }
        public static TInt64 operator %(TInt64 x, TInt64 y) { return new TInt64(x.value % y.value); }


        //TInt.Null可以和其他TInt 对象比较操作==,!=；
        public static bool operator ==(TInt64 x, TInt64 y) { return x.value == y.value; }
        public static bool operator !=(TInt64 x, TInt64 y) { return x.value != y.value; }
        public static bool operator >(TInt64 x, TInt64 y) { return x.value > y.value; }
        public static bool operator <(TInt64 x, TInt64 y) { return x.value < y.value; }
        public static bool operator >=(TInt64 x, TInt64 y) { return x.value >= y.value; }
        public static bool operator <=(TInt64 x, TInt64 y) { return x.value <= y.value; }
        #endregion

        #region 基类属性重载
        protected TableFieldInt64Property property;
        /// <summary>
        /// 获取或设置字段在对象树中的 属性列表
        /// </summary>
        public virtual TableFieldInt64Property Property { get; set; }
        /// <summary>
        /// 获取字段在对象数中的 属性列表
        /// </summary>
        public override TableFieldProperty BaseProperty
        { get { return this.Property; } }
        #endregion

    }
    #endregion

    #region TString
    /// <summary>
    /// String 数据类型
    /// </summary>
    public class TString : TableFieldType
    {
        #region 辅助方法
        public static string ObjectToString(object value)
        {
            TString tString = new TString();
            tString.ObjectValue = value;
            return tString.StringValue;
        }
        public static object StringToObject(string value)
        {
            TString tString = new TString();
            tString.StringValue = value;
            return tString.ObjectValue;
        }
        #endregion

        #region 默认值
        public static readonly TString DefaultValue = new TString("");
        /// <summary>
        /// 获取默认值
        /// </summary>
        /// <returns></returns>
        public override TableFieldType GetDefaultValue()
        {
            return DefaultValue;
        }
        /// <summary>
        /// 获取是否为默认值
        /// </summary>
        public override bool IsDefaultValue
        {
            get
            {
                return (bool)this;
            }
        }
        #endregion

        #region 构造函数
        private string value;
        public TString() { this.value = DefaultValue.Value; }
        public TString(string value) { this.value = value; }
        #endregion

        #region 属性

        public string Value { get { return this.value; } set { SetValue ( value); } }
        public string ValueString { get { return this.value.ToString(); } }

        public System.Type ControlType { get { return ((new ComboBox()).GetType()); } }
        public int Length { get { return this.value.Length; } }
        #endregion

        #region 强制转换
        public static implicit operator TString(string x) { return new TString(x); }
        public static explicit operator string(TString x) { return x.Value; }
        public static explicit operator bool(TString x) { return x.Value == DefaultValue.Value; }
        #endregion

        #region 操作符重载
        public static TString operator +(TString x, TString y) { return new TString(x.value + y.value); }
        public static bool operator ==(TString x, TString y) { return x.value == y.value; }
        public static bool operator !=(TString x, TString y) { return x.value != y.value; }
        #endregion

        #region 父类方法重载
        public override bool Equals(object obj) { return base.Equals(obj); }
        public override int GetHashCode() { return base.GetHashCode(); }
        public override string ToString() { return this.StringValue; }
        public override System.Type ValueType { get { return typeof(string); } }
        /// <summary>
        /// string 类型
        /// </summary>
        public override object ObjectValue { get { return this.value; } set { SetValue(value); } }
        public override string StringValue
        {
            get { return this.value; }
            set { SetValue(value); }
        }


        public override void SetValue(object value)
        {
            base.SetValue(value);

            if (value.GetType().IsSubclassOf(typeof(TableFieldType)))
            {
                this.value = ((TableFieldType)value).StringValue;
                return;
            }

            //if (value == null || value.ToString() == "")
            //{
            //    value = DefaultValue.Value ;
            //}
            if (this.Property != null && value.ToString().Length > this.Property.StringSize)
                value = value.ToString().Substring(0, Convert.ToInt32(this.Property.StringSize) - 1);
            try
            {
                this.value = Convert.ToString(value);
            }
            catch
            {
                throw new InvalidCastException("类型转换不成功:" + value.ToString() + " TO:" + this.GetType().Name);
            }
        }

        protected TableFieldStringProperty property;
        /// <summary>
        /// 表字段在对象树中的 属性列表
        /// </summary>
        public virtual RuleCode.ObjectTree.TableFieldStringProperty Property { get; set; }

        public override TableFieldProperty BaseProperty
        {
            get
            {
                return this.Property;
            }
        }

        #endregion

    }
    #endregion

    #region TFloat
    /// <summary>
    ///  float数据类型 
    /// </summary>
    public class TFloat : TableFieldType
    {

        #region 辅助方法
        public static string ObjectToString(object value)
        {
            TFloat tFloat = new TFloat();
            tFloat.ObjectValue = value;
            return tFloat.StringValue;
        }
        public static object StringToObject(string value)
        {
            TFloat tFloat = new TFloat();
            tFloat.StringValue = value;
            return tFloat.ObjectValue;
        }
        #endregion

        #region 默认值
        public static readonly TFloat DefaultValue = new TFloat(0);
        /// <summary>
        /// 获取是否为默认值
        /// </summary>
        public override bool IsDefaultValue
        {
            get
            {
                return (bool)this;
            }
        }
        /// <summary>
        /// 获取默认值
        /// </summary>
        /// <returns></returns>
        public override TableFieldType GetDefaultValue()
        {
            return DefaultValue;
        }
        #endregion

        #region TFloat和float之间的显式转换
        public static implicit operator TFloat(float x) { return new TFloat(x); }
        public static explicit operator float(TFloat x) { return x.Value; }
        public static explicit operator bool(TFloat x) { return x.Value == DefaultValue.Value; }
        #endregion

        #region  构造函数
        private float value;
        public TFloat() { this.value = DefaultValue.Value; }
        public TFloat(float val) { this.value = val; }
        #endregion

        #region 方法重载
        public override bool Equals(object obj) { return base.Equals(obj); }
        public override int GetHashCode() { return base.GetHashCode(); }
        public override string ToString() { return this.StringValue; }
        /// <summary>
        /// 设置当前的值 TFloat float 以及其他可以转换为 float 类型
        /// </summary>
        /// <param name="value"></param>
        public override void SetValue(object value)
        {
            base.SetValue(value);

            if (value.GetType().IsSubclassOf(typeof(TableFieldType)))
            {
                this.value = Convert.ToSingle(((TableFieldType)value).ObjectValue);
                return;
            }

            //if (value == null || value.ToString() == "")
            //    value = DefaultValue.Value;
            try
            {
                this.value = Convert.ToSingle(value);
            }
            catch
            {
                throw new InvalidCastException("类型转换不成功:" + value.ToString() + " TO:" + this.GetType().Name);
            }
        }
        /// <summary>
        /// 获取值类型 typeof(float)
        /// </summary>
        public override System.Type ValueType { get { return typeof(float); } }
        /// <summary>
        /// 获取或设置 非字符串类型的值
        /// </summary>
        public override object ObjectValue { get { return this.value; } set { SetValue(value); } }
        /// <summary>
        /// 获取或设置字符串类型的值
        /// </summary>
        public override string StringValue
        {
            get { return this.value.ToString(); }
            set { SetValue(value); }
        }
        #endregion

        #region 属性
        public float Value { get { return this.value; } set { SetValue(value); } }
        public string ValueString { get { return this.value.ToString(); } }
        public System.Type ControlType { get { return ((new ComboBox()).GetType()); } }
        #endregion

        #region 运算符
        public static TFloat operator +(TFloat x) { return x; }
        public static TFloat operator -(TFloat x) { return new TFloat(-x.value); }

        public static TFloat operator +(TFloat x, TFloat y) { return new TFloat(x.value + y.value); }
        public static TFloat operator -(TFloat x, TFloat y) { return new TFloat(x.value - y.value); }
        public static TFloat operator *(TFloat x, TFloat y) { return new TFloat(x.value * y.value); }


        //TInt.Null可以和其他TInt 对象比较操作==,!=；
        public static bool operator ==(TFloat x, TFloat y) { return x.value == y.value; }
        public static bool operator !=(TFloat x, TFloat y) { return x.value != y.value; }
        public static bool operator >(TFloat x, TFloat y) { return x.value > y.value; }
        public static bool operator <(TFloat x, TFloat y) { return x.value < y.value; }
        public static bool operator >=(TFloat x, TFloat y) { return x.value >= y.value; }
        public static bool operator <=(TFloat x, TFloat y) { return x.value <= y.value; }
        #endregion

    }
    #endregion

    #region TDouble
    /// <summary>
    /// double数据类型 
    /// </summary>
    public class TDouble : TableFieldType
    {
        #region 辅助方法
        public static string ObjectToString(object value)
        {
            TDouble tDouble = new TDouble();
            tDouble.ObjectValue = value;
            return tDouble.StringValue;
        }
        public static object StringToObject(string value)
        {
            TDouble tDouble = new TDouble();
            tDouble.StringValue = value;
            return tDouble.ObjectValue;
        }
        #endregion

        #region 默认值
        public static readonly TDouble DefaultValue = new TDouble(0.0);
        /// <summary>
        /// 获取 是否为默认值
        /// </summary>
        public override bool IsDefaultValue
        {
            get
            {
                return (bool)this;
            }
        }
        /// <summary>
        /// 获取默认值 
        /// </summary>
        /// <returns> TDouble类型</returns>
        public override TableFieldType GetDefaultValue()
        {
            return DefaultValue;
        }
        #endregion

        #region 构造函数
        private double value;
        public TDouble() { this.value = DefaultValue.Value; }
        public TDouble(double val) { this.value = val; }
        #endregion

        #region 属性

        public double Value { get { return this.value; } set { SetValue(value); } }
        public string ValueString { get { return this.value.ToString(); } }

        public System.Type ControlType { get { return ((new ComboBox()).GetType()); } }
        #endregion

        #region TFloat和float之间的显式转换
        public static implicit operator TDouble(double x) { return new TDouble(x); }
        public static explicit operator double(TDouble x) { return x.Value; }
        public static explicit operator bool(TDouble x) { return x.Value == DefaultValue.Value; }
        #endregion

        #region 运算符
        public static TDouble operator +(TDouble x) { return x; }
        public static TDouble operator -(TDouble x) { return new TDouble(-x.value); }

        public static TDouble operator +(TDouble x, TDouble y) { return new TDouble(x.value + y.value); }
        public static TDouble operator -(TDouble x, TDouble y) { return new TDouble(x.value - y.value); }
        public static TDouble operator *(TDouble x, TDouble y) { return new TDouble(x.value * y.value); }


        //TInt.Null可以和其他TInt 对象比较操作==,!=；
        public static bool operator ==(TDouble x, TDouble y) { return x.value == y.value; }
        public static bool operator !=(TDouble x, TDouble y) { return x.value != y.value; }
        public static bool operator >(TDouble x, TDouble y) { return x.value > y.value; }
        public static bool operator <(TDouble x, TDouble y) { return x.value < y.value; }
        public static bool operator >=(TDouble x, TDouble y) { return x.value >= y.value; }
        public static bool operator <=(TDouble x, TDouble y) { return x.value <= y.value; }
        #endregion

        #region 方法重载
        public override bool Equals(object obj) { return base.Equals(obj); }
        public override int GetHashCode() { return base.GetHashCode(); }
        public override string ToString() { return this.StringValue; }
        /// <summary>
        /// Double 类型
        /// </summary>
        public override System.Type ValueType { get { return typeof(double); } }
        /// <summary>
        /// 获取或设置当前非字符串类型的值 double类型
        /// </summary>
        public override object ObjectValue { get { return this.value; } set { SetValue(value); } }
        /// <summary>
        /// 获取或设置当前的字符串格式的值
        /// </summary>
        public override string StringValue
        {
            get { return this.value.ToString(); }
            set { SetValue(value); }

        }

        /// <summary>
        /// 设置当前的值 TDouble double或者其他可以转换为double类型的值
        /// </summary>
        /// <param name="value"></param>
        public override void SetValue(object value)
        {
            base.SetValue(value);

            if (value.GetType().IsSubclassOf(typeof(TableFieldType)))
            {
                this.value = Convert.ToDouble(((TableFieldType)value).ObjectValue);
                return;
            }

            //if (value == null || value.ToString() == "")
            //    value = DefaultValue.Value;

            try
            {
                this.value = Convert.ToDouble(value);
            }
            catch
            {
                throw new InvalidCastException("类型转换不成功:" + value.ToString() + " TO:" + this.GetType().Name);
            }
        }
        protected TableFieldRealProperty property;
        /// <summary>
        /// 表字段在对象树中的 属性列表
        /// </summary>
        public virtual RuleCode.ObjectTree.TableFieldRealProperty Property { get; set; }
        public override TableFieldProperty BaseProperty
        {
            get
            {
                return this.Property;
            }
        }

        #endregion
    }
    #endregion

    #region TDate
    /// <summary>
    /// DateTime 数据类型
    /// </summary>
    public class TDate : TableFieldType
    {
        #region 辅助方法
        public static string ObjectToString(object value)
        {
            TDate tDate = new TDate();
            tDate.ObjectValue = value;
            return tDate.StringValue;
        }
        public static object StringToObject(string value)
        {
            TDate tDate = new TDate();
            tDate.StringValue = value;
            return tDate.ObjectValue;
        }
        #endregion

        #region 默认值
        public static readonly TDate DefaultValue = new TDate(DateTimePicker.MinimumDateTime);
        /// <summary>
        /// 获取是否为默认值
        /// </summary>
        public override bool IsDefaultValue
        {
            get
            {
                return (bool)this;
            }
        }
        /// <summary>
        /// 获取默认值
        /// </summary>
        /// <returns>TDate类型</returns>
        public override TableFieldType GetDefaultValue()
        {
            return DefaultValue;
        }
        #endregion

        #region 字段
        private static int minYear = 1900;
        private static int maxYear = 2500;
        private int year;
        private int month;
        private int day;
        private DateTime value;
        #endregion

        #region 构造函数
        public TDate() { this.Value = DefaultValue.Value; }
        public TDate(int y, int m, int d)
        {

            if (ValidateDate(y, m, d))
            {
                this.year = y;
                this.month = m;
                this.day = d;
            }
        }
        public TDate(DateTime dTime) { this.day = dTime.Day; this.month = dTime.Month; this.year = dTime.Year; this.value = dTime; }
        #endregion

        #region 属性
        //public System.Type ControlType { get { return ((new DateTimePicker()).GetType()); } }
        public int DayOfYear { get { return (new DateTime(this.Year, this.Month, this.Day)).DayOfYear; } }

        public int Year { get { return this.year; } }
        public int Month { get { return this.month; } }
        public int Day { get { return this.day; } }
        public DateTime Value
        {
            get
            {
                if (this.value == null)
                    this.value = new DateTime(year, month, day);
                return this.value;
            }
            set
            {
                this.value = value;
            }
        }
        public bool IsLeapyear { get { return (((this.Year % 4 == 0) && (this.Year % 100 != 0)) || ((this.Year % 100 == 0) && (this.Year % 400 == 0))); } }

        public string Date
        {
            get
            {
                string date = this.Year.ToString() + "-";
                if (this.Month < 10)
                    date = date + "0";
                date = date + this.Month.ToString() + "-";
                if (this.day < 10)
                    date = date + "0";
                date = date + this.Day.ToString();
                return date;
            }
            set
            {

                DateTime dTime = Convert.ToDateTime(value);
                this.year = dTime.Year;
                this.month = dTime.Month;
                this.day = dTime.Day;
            }
        }
        #endregion

        #region 验证函数
        //判断输入的年月日是否符合逻辑！
        public bool ValidateDate(int y, int m, int d)
        {
            bool result = (y >= minYear) && (y <= maxYear);
            result = result && (m >= 1) && (m <= 12);
            result = result && (d > 0);
            List<int> Days31 = new List<int>(new int[] { 1, 3, 5, 7, 8, 10, 12 });
            List<int> Days30 = new List<int>(new int[] { 4, 6, 9, 11 });

            if (Days31.Contains(m))
                return result && (d < 32);
            else if (Days30.Contains(m))
                return result && (d < 31);
            else if (m == 2)
                if (((y % 4 == 0) && (y % 100 != 0)) || ((y % 100 == 0) && (y % 400 == 0)))
                    return result && (d < 30);
                else
                    return result && (d < 29);
            if (!result)
                throw new InvalidTimeZoneException("你输入的日期超出范围！！");
            else
                return result;
        }
        #endregion

        #region 操作符以及转换
        public static explicit operator bool(TDate x) { return x.Value == DefaultValue.Value; }
        public static implicit operator TDate(DateTime dTime) { return new TDate(dTime); }
        public static explicit operator DateTime(TDate tDate) { return tDate.value; }

        public static bool operator ==(TDate x, TDate y) { return (x.Day == y.Day && x.Month == y.Month && x.Year == y.Year); }
        public static bool operator !=(TDate x, TDate y) { return x.Day != y.Day || x.Month != y.Month || x.Year != y.Year; }
        public static bool operator >(TDate x, TDate y) { return (x.Year > y.Year) || (x.Year == y.Year && x.Month > y.Month) || (x.Year == y.Year && x.Month == y.Month && x.Day > y.Day); }
        public static bool operator <(TDate x, TDate y) { return (x.Year < y.Year) || (x.Year == y.Year && x.Month < y.Month) || (x.Year == y.Year && x.Month == y.Month && x.Day < y.Day); }
        public static bool operator >=(TDate x, TDate y) { return (x.Year >= y.Year) || (x.Year == y.Year && x.Month >= y.Month) || (x.Year == y.Year && x.Month == y.Month && x.Day >= y.Day); }
        public static bool operator <=(TDate x, TDate y) { return (x.Year <= y.Year) || (x.Year == y.Year && x.Month <= y.Month) || (x.Year == y.Year && x.Month == y.Month && x.Day <= y.Day); }

        public static int operator -(TDate x, TDate y) { return x.SubStract(y); }
        #endregion

        #region  DateTime 方法
        public TDate AddDays(int days)
        {
            DateTime dTime = new DateTime(this.Year, this.Month, this.Day);
            dTime.AddDays(days);
            return (TDate)(dTime);
        }
        public TDate AddMonths(int months)
        {
            DateTime dTime = new DateTime(this.Year, this.Month, this.Day);
            dTime.AddMonths(months);
            return (TDate)(dTime);
        }
        public TDate AddYears(int years)
        {
            DateTime dTime = new DateTime(this.Year, this.Month, this.Day);
            dTime.AddYears(years);
            return (TDate)(dTime);
        }
        public int SubStract(TDate tDate)
        {
            TimeSpan tSpan = new TimeSpan();
            DateTime dTime = new DateTime(this.Year, this.Month, this.Day);
            tSpan = dTime.Subtract((DateTime)(tDate));
            return (int)(tSpan.TotalDays);
        }
        public TDate SubStract(int days)
        {
            DateTime dTime = new DateTime(this.Year, this.Month, this.Day);
            return (TDate)(dTime.Subtract(new TimeSpan(days, 0, 0, 0)));
        }
        #endregion

        #region  方法重载
        public string ToShortString() { return this.Date; }
        public string ToLongString() { return this.value.ToString(); }
        public override string ToString() { return this.StringValue; }
        public override bool Equals(object obj) { return base.Equals(obj); }
        public override int GetHashCode() { return base.GetHashCode(); }
        /// <summary>
        /// 获取当前的值的类型 DateTime类型
        /// </summary>
        public override System.Type ValueType { get { return typeof(DateTime); } }
        /// <summary>
        /// 获取或设置非字符串类型的值 DateTime 类型 
        /// </summary>
        public override object ObjectValue
        {
            get { return (DateTime)this; }
            set { SetValue(value); }
        }
        /// <summary>
        /// 获取或设置当前字符串格式的值
        /// </summary>
        public override string StringValue
        {
            get { return this.value.ToShortDateString(); }
            set { SetValue(value); }
        }

        /// <summary>
        /// 设置当前的值 TDate，DateTime 或其他可以转化为DateTime类型的值
        /// </summary>
        /// <param name="value"></param>
        public override void SetValue(object value)
        {
            base.SetValue(value);

            if (value.GetType().IsSubclassOf(typeof(TableFieldType)))
            {
                this.Value = Convert.ToDateTime(((TableFieldType)value).ObjectValue);
                return;
            }
            //if (value == null || value.ToString() == "")
            //{
            //    value = DefaultValue.Value;
            //}

            try
            {
                this.value = Convert.ToDateTime(value);
                this.year = (Convert.ToDateTime(value)).Year;
                this.month = (Convert.ToDateTime(value)).Month;
                this.day = (Convert.ToDateTime(value)).Day;
            }
            catch
            {
                throw new InvalidCastException("类型转换不成功:" + value.ToString() + " TO:" + this.GetType().Name);
            }
        }


        protected TableFieldDateProperty property;
        /// <summary>
        /// 表字段在对象树中的 属性列表
        /// </summary>
        public virtual RuleCode.ObjectTree.TableFieldDateProperty Property { get; set; }
        public override TableFieldProperty BaseProperty
        {
            get
            {
                return this.Property;
            }
        }
        #endregion

    }
    #endregion

    #region TTime
    /// <summary>
    /// 时间类型，数据类型为 int
    /// </summary>
    public class TTime : TableFieldType
    {
        #region 辅助方法
        public static string ObjectToString(object value)
        {
            TTime tTime = new TTime();
            tTime.ObjectValue = value;
            return tTime.StringValue;
        }
        public static object StringToObject(string value)
        {
            TTime tTime = new TTime();
            tTime.StringValue = value;
            return tTime.ObjectValue;
        }
        #endregion

        #region 默认值
        public static readonly TTime DefaultValue = new TTime(0);
        /// <summary>
        /// 获取 是否为默认值
        /// </summary>
        public override bool IsDefaultValue { get { return (bool)this; } }
        /// <summary>
        /// 获取默认值
        /// </summary>
        /// <returns>TTime类型</returns>
        public override TableFieldType GetDefaultValue()
        {
            return DefaultValue;
        }
        #endregion

        #region 字段
        private int hour;
        private int minute;
        private int second;

        private int time;
        #endregion

        #region 构造函数
        public TTime() { this.Value = DefaultValue.Value; }
        public TTime(int x)
        {

            if (x >= 0)
            {
                int y = x % (60 * 60 * 24);
                this.hour = y / (60 * 60);
                this.minute = (y % (60 * 60)) / 60;
                this.second = (y % (60 * 60)) % 60;
            }
            else
            {
                throw new ArgumentException("输入参数太大或太小！");
            }

        }
        public TTime(int h, int m, int s)
        {

            if (ValidateTime(h, m, s))
            {
                this.hour = h; this.minute = m; this.second = s;
                time = h * 60 * 60 + m * 60 + s;
            }
        }
        public TTime(string timeString)
        {

            this.Time = timeString;
        }
        #endregion

        #region 验证函数
        public bool ValidateTime(int h, int m, int s)
        {
            bool result = (h >= 0 && h < 24) && (m >= 0 && m < 60) && (s >= 0 && s < 60);
            if (!result)
                throw new InvalidTimeZoneException("你输入的时间超出范围！！");
            else
                return result;
        }
        #endregion

        #region 属性

        public int Hour { get { return this.hour; } }
        public int Minute { get { return this.minute; } }
        public int Second { get { return this.second; } }
        public int Value { get { return this.TimeOffset; } set { this.TimeOffset = value; } }
        //public System.Type ControlType { get { return ((new DateTimePicker()).GetType()); } }

        public string Time
        {
            get
            {
                string time0 = "";
                if (this.hour < 10)
                    time0 += "0";
                time0 += this.hour.ToString() + ":";
                if (this.minute < 10)
                    time0 += "0";
                time0 += this.minute.ToString() + ":";
                if (this.second < 10)
                    time0 += "0";
                time0 += this.second.ToString();
                return time0;
            }
            set
            {

                string pattern = "^(([0-1][0-9]|([0-9]))|(2[0-3])):(([0-5][0-9])|([0-9])):(([0-5][0-9])|([0-9]))$";//"[[0-1][0-9]]|[2[0-3]]|[0-9]:[[0-5][0-9]]|[0-9]:[[0-5][0-9]]|[0-9]";
                if (Regex.IsMatch(value, pattern))
                {
                    string timestring = value;
                    string[] time0 = timestring.Split(new char[] { ':' });
                    string hourString = time0[0];
                    string minuterString = time0[1];
                    string secondString = time0[2];
                    if (hourString == "00" )
                        this.hour = 0;
                    else
                        this.hour = Convert.ToInt32(hourString.TrimStart(new char[] { '0' }));

                    if (minuterString == "00" )
                        this.minute = 0;
                    else
                        this.minute = Convert.ToInt32(minuterString.TrimStart(new char[] { '0' }));

                    if (secondString == "00")
                        this.second = 0;
                    else
                        this.second = Convert.ToInt32(secondString.TrimStart(new char[] { '0' }));
                    this.time = this.Hour * 60 * 60 + this.Minute * 60 + this.second;

                }
                else
                {
                    throw new InvalidCastException("格式不正确，不能转换");
                }
            }
        }

        public int TimeOffset
        {
            get { return this.time; }
            set
            {

                int x = value;
                if (x >= 0)
                {
                    x = x % (60 * 60 * 24);
                    this.hour = x / (60 * 60);
                    this.minute = (x % (60 * 60)) / 60;
                    this.second = (x % (60 * 60)) % 60;
                    this.time = value;
                }
                else
                {
                    throw new ArgumentException("输入参数太大或太小！");
                }

            }
        }
        #endregion

        #region 函数重载
        public override bool Equals(object obj) { return base.Equals(obj); }
        public override int GetHashCode() { return base.GetHashCode(); }
        public override string ToString() { return this.StringValue; }
        /// <summary>
        /// 获取当前的值的类型 int类型
        /// </summary>
        public override System.Type ValueType { get { return typeof(int); } }
        /// <summary>
        /// 获取或设置当前非字符串格式的值 int类型
        /// </summary>
        public override object ObjectValue
        {
            get { return this.TimeOffset; }
            set
            {
                SetValue(value);
            }
        }
        /// <summary>
        /// 获取或设置当前字符串格式的值 格式：01:01:01
        /// </summary>
        public override string StringValue
        {

            get { return this.Time; }
            set { this.Time = value; }
        }
        protected TableFieldTimeProperty property;
        /// <summary>
        /// 表字段在对象树中的 属性列表
        /// </summary>
        public virtual RuleCode.ObjectTree.TableFieldTimeProperty Property { get; set; }
        public override TableFieldProperty BaseProperty
        {
            get
            {
                return this.Property;
            }
        }
        /// <summary>
        /// 设置当前的值 TTime类型 int 类型 或者 可以
        /// </summary>
        /// <param name="value"></param>
        public override void SetValue(object value)
        {
            base.SetValue(value);

            if (value.GetType().IsSubclassOf(typeof(TableFieldType)))
            {
                this.Value = Convert.ToInt32(((TableFieldType)value).ObjectValue);
                return;
            }

            //if (value == null || value.ToString() == "")
            //    value = DefaultValue.Value;

            try
            {
                if (value.GetType() == typeof(int))
                {
                    this.TimeOffset = ((int)value);
                }
                else if (value.GetType() == typeof(string) && value.ToString().Contains(":"))
                {
                    this.Time = value.ToString();
                }
                else
                    this.TimeOffset = Convert.ToInt32(value);
            }
            catch
            {
                throw new InvalidCastException("类型转换不成功:" + value.ToString() + " TO:" + this.GetType().Name);
            }
            //base.SetValue(value);
        }
        #endregion

        #region 操作符和类型转换
        public static explicit operator bool(TTime x) { return x.Value == DefaultValue.Value; }
        public static implicit operator TTime(int x) { return new TTime(x); }
        public static explicit operator int(TTime tTime) { return tTime.TimeOffset; }//

        public static bool operator ==(TTime x, TTime y) { return x.TimeOffset == y.TimeOffset; }
        public static bool operator !=(TTime x, TTime y) { return x.TimeOffset != y.TimeOffset; }
        public static bool operator >(TTime x, TTime y) { return x.TimeOffset > y.TimeOffset; }
        public static bool operator <(TTime x, TTime y) { return x.TimeOffset < y.TimeOffset; }
        public static bool operator >=(TTime x, TTime y) { return x.TimeOffset >= y.TimeOffset; }
        public static bool operator <=(TTime x, TTime y) { return x.TimeOffset <= y.TimeOffset; }

        public static TTime operator -(TTime x, TTime y) { return new TTime(x.TimeOffset - y.TimeOffset); }
        #endregion

        #region TTime 方法
        public TTime AddSeconds(int seconds)
        {
            this.TimeOffset += seconds;
            this.TimeOffset = this.TimeOffset % (60 * 60 * 24);
            return this;
        }
        public TTime AddMinutes(int minutes)
        {
            this.TimeOffset += minutes * 60;
            this.TimeOffset = this.TimeOffset % (60 * 60 * 24);
            return this;
        }
        public TTime AddHours(int hours)
        {
            this.TimeOffset += hours * 60 * 60;
            this.TimeOffset = this.TimeOffset % (60 * 60 * 24);
            return this;
        }
        #endregion

       

    }
    #endregion

    #region TContainer
    /// <summary>
    /// byte []数据类型的表字段基类型
    /// </summary>
    public class TContainer : TableFieldType
    {
        #region 辅助方法
        public static string ObjectToString(object value)
        {
            TContainer tContainer = new TContainer();
            tContainer.ObjectValue = value;
            return tContainer.StringValue;
        }
        public static object StringToObject(string value)
        {
            TContainer tContainer = new TContainer();
            tContainer.StringValue = value;
            return tContainer.ObjectValue;
        }
        #endregion

        #region 默认值
        public static int MaxLength = int.MaxValue;
        public static readonly TContainer DefaultValue = new TContainer(new byte[] { new byte() });
        public override TableFieldType GetDefaultValue()
        {
            return DefaultValue;
        }
        public override bool IsDefaultValue
        {
            get
            {
                return (bool)this;
            }
        }
        #endregion

        #region 构造函数
        public TContainer() { this.Value = DefaultValue.Value; }
        public TContainer(byte[] value) { this.value = value; }
        public TContainer(Container value) { this.Value = System.Text.Encoding.Default.GetBytes(value.ToString()); }
        #endregion

        #region  属性
        private byte[] value;
        public byte[] Value { get { return this.value; } set { this.value = value; } }
        public string ValueString { get { return System.Text.Encoding.Default.GetString(this.value); } }

        public System.Type ControlType { get { return ((new ComboBox()).GetType()); } }
        #endregion

        #region 操作符和类型转换
        public static explicit operator bool(TContainer x) { return x.Value == DefaultValue.Value; }
        public static implicit operator TContainer(byte[] x) { return new TContainer(x); }
        public static explicit operator byte[](TContainer x) { return x.value; }
        //
        public static bool operator ==(TContainer x, TContainer y) { return x.value == y.value; }
        public static bool operator !=(TContainer x, TContainer y) { return x.value != y.value; }
        #endregion

        #region 方法重载
        public override bool Equals(object obj) { return base.Equals(obj); }
        public override int GetHashCode() { return base.GetHashCode(); }
        public override string ToString() { return this.StringValue; }
        /// <summary>
        /// byte [] 类型 TContainer
        /// </summary>
        public override object ObjectValue { get { return this.value; } set { SetValue(value); } }
        /// <summary>
        /// 获取或设置当前值的字符串格式值
        /// </summary>
        public override string StringValue { get { return this.ValueString; } set { SetValue(value); } }
        /// <summary>
        /// 获取当前值的类型 byte[]
        /// </summary>
        public override System.Type ValueType { get { return typeof(byte[]); } }

        /// <summary>
        /// 设置当前的值 
        /// </summary>
        /// <param name="value">TContainer类型,byte[]类型，可以转化为 byte[]类型的string</param>
        public override void SetValue(object value)
        {
            base.SetValue(value);

            if (value.GetType().IsSubclassOf(typeof(TableFieldType)))
            {
                this.Value = System.Text.Encoding.Default.GetBytes(((TableFieldType)value).StringValue);
                return;
            }


            //if (value == null || value.ToString() == "")
            //    value = DefaultValue.Value;

            try
            {
                if (value.GetType() == typeof(byte[]))
                {
                    this.value = (byte[])value;
                }
                else
                    this.value = System.Text.Encoding.Default.GetBytes(value.ToString());
            }
            catch
            {
                throw new InvalidCastException("类型转换不成功:" + value.ToString() + " TO:" + this.GetType().Name + "!()");
            }
        }


        protected TableFieldContainerProperty property;
        /// <summary>
        /// 表字段在对象树中的 属性列表
        /// </summary>
        public virtual RuleCode.ObjectTree.TableFieldContainerProperty Property { get; set; }
        public override TableFieldProperty BaseProperty
        {
            get
            {
                return this.Property;
            }
        }
        #endregion
    }
    #endregion

    #region TEnum
    /// <summary>
    /// 数据类型为enum
    /// </summary>
    public class TEnum : TableFieldType
    {
        #region 辅助方法
        public static string ObjectToString(object value)
        {
            TEnum tEnum = new TEnum();
            tEnum.ObjectValue = value;
            return tEnum.StringValue;
        }
        public static object StringToObject(string value)
        {
            TEnum tEnum = new TEnum();
            tEnum.StringValue = value;
            return tEnum.ObjectValue;
        }
        #endregion

        #region  默认值
        public static readonly TEnum DefaultValue = new TEnum(0);
        public static explicit operator bool(TEnum x) { return x.Value == DefaultValue.Value; }
        public override TableFieldType GetDefaultValue()
        {
            return DefaultValue;
        }
        #endregion

        #region 构造函数
        private int value;
        public TEnum() { this.value = DefaultValue.Value; }
        public TEnum(int val) { this.value = val; }
        #endregion

        #region 属性
        public int Value { get { return this.value; } set { this.value = value; } }
        public string ValueString { get { return this.StringValue; } }
        public System.Type ControlType { get { return ((new ComboBox()).GetType()); } }
        #endregion

        #region TEnum和int之间的显式转换
        public static implicit operator TEnum(int x) { return new TEnum(x); }
        public static explicit operator int(TEnum x) { return x.Value; }
        #endregion

        #region 运算符


        //TEnum.Null可以和其他TEnum 对象比较操作==,!=；
        public static bool operator ==(TEnum x, TEnum y) { return x.value == y.value; }
        public static bool operator !=(TEnum x, TEnum y) { return x.value != y.value; }
        #endregion

        #region 方法重载
        public override bool Equals(object obj) { return base.Equals(obj); }
        public override int GetHashCode() { return base.GetHashCode(); }
        public override string ToString() { return this.StringValue; }

        public override System.Type ValueType { get { return typeof(int); } }
        /// <summary>
        /// 获取或设置非字符串格式的值
        /// </summary>
        public override object ObjectValue { get { return this.value; } set { SetValue(value); } }
        /// <summary>
        /// 获取或设置 字符串格式的值
        /// </summary>
        public override string StringValue
        {
            get
            {
                if (this.EnumatorProperty != null && this.EnumatorProperty.Count > 0 && this.Value < this.EnumatorProperty.Count)
                {
                    foreach (EnumatorProperty property in this.EnumatorProperty)
                    {
                        if (property.EnumValue == this.value)
                            return property.Name;
                    }
                }
                return "";
            }
            set
            {
                SetValue(value);
            }
        }

        /// <summary>
        /// 设置当前的值
        /// </summary>
        /// <param name="value">TEnum的子类，int类型或者可以转化为int类型的字符串</param>
        public override void SetValue(object value)
        {
            if (value.ToString() == "")
            {
                value = "0";
            }
            base.SetValue(value);

            if (value.GetType().IsSubclassOf(typeof(TableFieldType)))
            {
                this.Value = Convert.ToInt32(((TableFieldType)value).ObjectValue);
                return;
            }

            //if (value == null || value.ToString() == "")
            //    value = DefaultValue.Value ;

            try
            {
                this.value = Convert.ToInt32(value);
            }
            catch
            {
                if (value.GetType() == typeof(string))
                {
                    bool result = false;
                    foreach (EnumatorProperty property in this.EnumatorProperty)
                    {
                        if (property.Name == value.ToString())
                        {
                            this.value = property.EnumValue;
                            result = true;
                            break;
                        }
                    }
                    if (result == false)
                        throw new InvalidCastException("类型[" + this.GetType().ToString() + "]中不存在这样的枚举名：" + value);
                }
                else
                    throw new InvalidCastException("类型转换不成功:" + value.ToString() + " TO:" + this.GetType().Name + "! (整数)...");
            }

        }

        public string Text
        {
            get
            {
                foreach (EnumatorProperty property in EnumatorProperty)
                {
                    if (property.EnumValue == value)
                        return property.Text;
                }
                return "";
            }
        }

        protected TableFieldEnumProperty property;
        /// <summary>
        /// 表字段在对象树中的 属性列表
        /// </summary>
        public virtual TableFieldEnumProperty Property { get; set; }
        public override TableFieldProperty BaseProperty
        { get { return this.Property; } }



        protected List<EnumatorProperty> enumatorProperty;
        public virtual List<EnumatorProperty> EnumatorProperty { get; set; }
        private System.Type baseEnumType;
        public System.Type BaseEnumType
        {
            get { return this.baseEnumType; }
            set { this.baseEnumType = value; }
        }
        #endregion

        #region 静态方法
        /// <summary>
        /// 获取枚举类型的 显示的文本 和 值 对
        /// </summary>
        /// <param name="enumFieldType">枚举字段类型</param>
        /// <returns></returns>
        public static Dictionary<string, int> GetEnumatorDictionary(System.Type enumFieldType)
        {
            Dictionary<string, int> enumatorDictionary = new Dictionary<string, int>();
            if (enumFieldType.BaseType == typeof(TEnum))
            {
                TEnum enumField = Activator.CreateInstance(enumFieldType) as TEnum;
                foreach (EnumatorProperty enumatorProperty in enumField.EnumatorProperty)
                {
                    enumatorDictionary.Add(SystemLanguage.Instance.GetLabel(enumatorProperty.Label), enumatorProperty.EnumValue);
                }
            }
            return enumatorDictionary;
        }
        #endregion
    }
    #endregion

}

